package com.itWaiLong;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Text {
    public static String md5(String input){

        try{
            //获取md5实例对象
            MessageDigest md = MessageDigest.getInstance("MD5");

            //计算哈希值
            byte[] digest = md.digest(input.getBytes());

            // 将字节数组转换为大整数
            BigInteger number = new BigInteger(1, digest);

            //将大整数转为十六进制
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

}
class LongTest{
    public static void main(String[] args) {
        Md5Text md5 = new Md5Text();
        System.out.println(Md5Text.md5("123456"));
    }
}